package com.mt.yanyou;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mt.yanyou.adapter.MyPagerAdapter;
import com.mt.yanyou.entity.TabEntity;
import com.mt.yanyou.fragment.PersonalFragment;
import com.mt.yanyou.fragment.StudentFragment;
import com.mt.yanyou.fragment.test1;
import com.mt.yanyou.fragment.test2;
import com.mt.yanyou.fragment.test3;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    String ARG_PARAM1 = "param1";
    //底部导航栏文字信息
    private String[] mTitles = {"首页", "自习","学习" ,"单词","我的"};
    //没有选中图片的状态
    private int[] mIconUnselectIds = {
            R.mipmap.help_nosel, R.mipmap.location_nosel,R.mipmap.study_sel,R.mipmap.health_nosel,
            R.mipmap.tool_nosel };
    //选中图片的状态
    private int[] mIconSelectIds = {
            R.mipmap.help_sel, R.mipmap.location_sel,R.mipmap.study_nosel,R.mipmap.health_sel,
            R.mipmap.tool_sel };
 
    private ViewPager viewPager;
    private CommonTabLayout commonTabLayout;
    //存fragment的数组对象
    private ArrayList<Fragment> mFragment = new ArrayList<>();
    //存导航栏图片，文字的选中或者不选择信息
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init_view();
        add_fragment();
        add_commonTabLayout();
        //为viewPager添加适配器
        FragmentManager fragmentManager=getSupportFragmentManager();
        viewPager.setAdapter(new MyPagerAdapter(fragmentManager,mTitles,mFragment));
    }

    private void init_view() {
        //获取xml信息
        viewPager=findViewById(R.id.viewpager);
        commonTabLayout=findViewById(R.id.commonTabLayout);
    }


    /**
     * 为fragment添加数据
     */
    private void add_fragment() {
        mFragment.add(test1.newInstance(ARG_PARAM1,"param1"));
        mFragment.add(test2.newInstance(ARG_PARAM1,"param1"));
        mFragment.add(test3.newInstance(ARG_PARAM1,"param1"));
        mFragment.add(StudentFragment.newInstance(ARG_PARAM1,"param1"));
        mFragment.add(PersonalFragment.newInstance(ARG_PARAM1,"param1"));
    }
    /**
     * 给导航栏添加图片和文字信息
     */
    private void add_commonTabLayout() {
        //添加选中或不选中状态
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        //为底部导航栏添加图片文字等数据
        commonTabLayout.setTabData(mTabEntities);
        //切换时，改变状态
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
